<?php
error_reporting(E_ALL ^E_NOTICE);
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

$_MAMBOTS->registerFunction( 'onPayProsessStart', 'jeProcessMoneyBookerStart' );
$_MAMBOTS->registerFunction( 'onPayProsessFinish', 'jeProcessMoneyBookerFinish' );


function jeProcessMoneyBookerFinish()
{
    Global $my, $database, $mosConfig_live_site;

    $processor = mosGetParam( $_REQUEST, 'processor'); 
    if($processor != '.moneybooker') return false;

    $order['id'] = mosGetParam( $_REQUEST, 'order_id');
    $order['gateway'] = $processor;
    $order['gateway_id'] = mosGetParam( $_REQUEST, 'mb_transaction_id');
    $order['price'] = mosGetParam( $_REQUEST, 'amount');
    $order['user_id']  =  mosGetParam( $_REQUEST, 'user_id');
    
    if(mosGetParam( $_REQUEST, 'status') == 2)
    {
    	$order['pay'] = 'success';
    	return $order;
    }
    if(mosGetParam( $_REQUEST, 'status') == 0)
    {
    	$order['pay'] = 'fail';
    	return $order;
    }
    return array();
}

function jeProcessMoneyBookerStart( $params )
{
    Global $my, $database, $mosConfig_live_site, $mosConfig_absolute_path, 
    $JCS_ORDER_COMPLETE, $JCS_ORDER_INCOMPLETE;
    
    if($params['processor'] != '.moneybooker') return;
    
    if (file_exists( $mosConfig_absolute_path.'/components/com_jcs/languages/'.$mosConfig_lang.'.php' ))
        include_once( $mosConfig_absolute_path.'/components/com_jcs/languages/'.$mosConfig_lang.'.php' );
    else
        require_once( $mosConfig_absolute_path.'/components/com_jcs/languages/english.php' );

    $query = "SELECT id"
    . "\n FROM #__mambots"
    . "\n WHERE element = 'joomlaequipment.payprocessor{$params[processor]}'"
    . "\n AND folder = 'system'"
    ;
    $database->setQuery( $query );
    $id     = $database->loadResult();
    $mambot = new mosMambot( $database );
    $mambot->load( $id );
    $botParams = new mosParameters( $mambot->params );
        
    $param['option']    = mosGetParam( $_REQUEST, 'option');
    $param['task']      = 'pay';
    $param['processor'] = $params['processor'];
    $param['order_id']  = $params['order_id'];
    $param['user_id'] = $my->id;
    $param['merchant_fields'] = 'option, task, order_id, processor, user_id';
    
    $return             = urlencode($mosConfig_live_site.'/index.php?option=com_jcs&smsg='.$JCS_ORDER_COMPLETE);
    $cancel_return      = urlencode($mosConfig_live_site.'/index.php?option=com_jcs&smsg='.$JCS_ORDER_INCOMPLETE);

    $param['pay_to_email'] 	 = urlencode($botParams->def( 'email'));
    $param['transaction_id'] = $params['order_id'].':'.date();
    $param['return_url']	 = $return;
    $param['cancel_url'] 	 = $cancel_return;
    $param['status_url'] 	 = $mosConfig_live_site.'/index.php?option=com_jcs&task=pay&processor=.moneybooker';
    $param['language'] 		 = $botParams->def('lang', 'EN');
    //$param['pay_from_email'] = $my->email;
    
    $param['amount']    = floatval($params['order_amount']);
    $param['amount2_description']    = $params['order_title'];
    $param['amount2']    = floatval($params['order_amount']);
    $param['currency'] = $botParams->def('currency', 'USD');
    $param['detail1_description'] = 'Subscription ID:';
    $param['detail1_text'] = $params['order_id'];
    
    if(!$param['pay_to_email'] || !$params['order_amount'])
    {
        echo "<P><h1>Error Payment Processing: some of the params passed are empty</h1></P>";
        return;
    }

    $url = 'https://www.moneybookers.com/app/payment.pl?'.jpArray2Url($param);
    //IF($botParams->def('demo'))
    //{
    //	$url = 'http://www.moneybookers.com/app/test_payment.pl';
    //}
    header("location: $url");
}

if(!defined('JP_IMPLODE'))
{
    define('JP_IMPLODE', 1);
    function jpArray2Url($param)
    {
        foreach($param AS $k => $v)
        {
            $out[] = "$k=$v";
        }
        return implode('&', $out );
    }
}



?>
